package com.rybbaby.tss.core.code.generate.view.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

/**
 * @description 视图配置
 * 
 * @author 谢进伟
 * 
 * @createDate 2016年11月13日 上午2:32:39
 */
public class Views {
	
	/** 配置文件路径 **/
	private String path;
	/** 模块名称 **/
	private String module;
	/** 模块描述 **/
	private String describe;
	/** 控制器 **/
	private List<Controller> controllers;
	
	public String getPath() {
		return path;
	}
	
	public void setPath(String path) {
		this.path = path;
	}
	
	public String getModule() {
		return module;
	}
	
	public void setModule(String module) {
		this.module = module;
	}
	
	public String getDescribe() {
		return describe;
	}
	
	public void setDescribe(String describe) {
		this.describe = describe;
	}
	
	public List<Controller> getControllers() {
		return controllers;
	}
	
	public void setControllers(List<Controller> controllers) {
		this.controllers = controllers;
	}
	
	/**
	 * 添加视图
	 * 
	 * @param controllerName
	 *            控制器名称
	 * @param key
	 *            视图key
	 * @param describe
	 *            视图描述
	 * @param path
	 *            视图相对路径
	 * @return
	 */
	public boolean addView(String controllerName , String key , String describe , String path) {
		View view = new View(key , describe , path);
		if(this.getControllers() == null) {
			this.setControllers(new ArrayList<Controller>());
		}
		Optional<Controller> controllerFindFirst = this.controllers.parallelStream().filter(new Predicate<Controller>() {
			
			@Override
			public boolean test(Controller t) {
				return t.getName().equals(controllerName);
			}
			
		}).findFirst();
		if(controllerFindFirst.isPresent()) {
			Controller t = controllerFindFirst.get();
			if(t.getViews() == null) {
				t.setViews(new ArrayList<View>());
			}
			Optional<View> viewFindFilter = t.getViews().parallelStream().filter(new Predicate<View>() {
				
				@Override
				public boolean test(View t) {
					return t.getKey().equals(key);
				}
				
			}).findFirst();
			if(viewFindFilter.isPresent()) {
				View oldView = viewFindFilter.get();
				oldView.setPath(path);
				oldView.setDescribe(describe);
				return true;
			} else {
				return t.getViews().add(view);
			}
		} else {
			List<View> views = new ArrayList<View>();
			views.add(view);
			Controller controller = new Controller();
			controller.setName(controllerName);
			controller.setViews(views);
			return this.controllers.add(controller);
		}
	}
}
